import { ref } from 'vue';
import { createGlobalState } from '@vueuse/core';
import {
  addDisableProperty,
  listToEnum,
  listToOption,
  treeToEnum,
} from '@/utils/utils';
import { getDictType } from '@/api/monitor';
import { getModel, getProjectTree } from '@/api/push';

export const deviceStore = createGlobalState(() => {
  // 审核状态
  const auditStatusOption = ref<Global.Option[]>([]);
  const auditStatusEnum = ref<Record<string, any>>({});
  const getAuditStatusDict = () => {
    getDictType('sys_audit_status').then((res: any) => {
      auditStatusOption.value = listToOption(
        res.data as any[],
        'dictLabel',
        'dictValue',
      );
      auditStatusEnum.value = listToEnum(
        res.data as any[],
        'dictValue',
        'dictLabel',
      );
    });
  };

  // 设备类型
  const deviceModeOption = ref<Global.Option[]>([]);
  const deviceModeEnum = ref<Record<string, any>>({});
  const getDeviceModeDict = () => {
    getDictType('sys_device_mode').then((res: any) => {
      deviceModeOption.value = listToOption(
        res.data as any[],
        'dictLabel',
        'dictValue',
      );
      deviceModeEnum.value = listToEnum(
        res.data as any[],
        'dictValue',
        'dictLabel',
      );
    });
  };

  // 获取-项目名称
  const projectList = ref([]);
  const getProjectList = async () => {
    const res = await getProjectTree();
    if (res.code == 200) {
      projectList.value = addDisableProperty(res.data);
    }
  };

  // 获取-设备型号
  const getModelOptions = ref<Global.Option[]>([]);
  const getModelEnum = ref<Record<string, any>>({});
  const getDeviceModels = async () => {
    const res = await getModel();
    if (res.code == 200) {
      getModelOptions.value = listToOption(
        res.data as any[],
        'model',
        'modelId',
      );
      getModelEnum.value = listToEnum(res.data as any[], 'modelId', 'model');
    }
  };

  // 获取-行业类型
  const deviceIndustryOption = ref<Global.Option[]>([]);
  const deviceIndustryEnum = ref<Record<string, any>>({});
  const getDeviceIndustryDict = () => {
    getDictType('device_industry').then((res: any) => {
      deviceIndustryOption.value = listToOption(
        res.data as any[],
        'dictLabel',
        'dictValue',
        'string',
      );
      deviceIndustryEnum.value = listToEnum(
        res.data as any[],
        'dictValue',
        'dictLabel',
      );
    });
  };
  const refreshStore = {
    info() {
      getAuditStatusDict();
      getDeviceModeDict();
      getProjectList();
      getDeviceModels();
      getDeviceIndustryDict();
    },
    device() {
      getDeviceModels();
      getDeviceIndustryDict();
    },
  };
  return {
    refreshStore,
    auditStatusEnum,
    auditStatusOption,
    deviceModeOption,
    deviceModeEnum,
    projectList,
    getModelOptions,
    getModelEnum,
    deviceIndustryOption,
    deviceIndustryEnum,
  };
});
